var Basket = Backbone.Model.extend({

});
BasketColl = Backbone.Collection.extend({
    model:Basket

});

var myBasket = new BasketColl();
BasketItemView = Backbone.View.extend({
    render:function () {
        var compiled_template = _.template($('#basketItemTemplate').html());
        this.el.innerHTML = compiled_template(this.model.toJSON());
        return this;
    }
});
var BasketView = Backbone.View.extend({
    initialize:function () {
        that = this;
        this.basketItemViews = [];

    },
    render:function () {
        this.el.innerHTML = "";
        this.collection.each(function (basketItem) {
            that.basketItemViews.push(new BasketItemView({
                model:basketItem
            }));
        });

        var list = "";

        _(that.basketItemViews).each(function (dv) {
            list = list.concat(dv.render().el.innerHTML);
        });



        if (list != "") {
            this.el.innerHTML= list;
            $(this.el).append("<div class='span12' onclick=\"$('#reqForm').modal();\" align=center ><button >Оформить заказ</button>");
        }else{
            $(this.el).append("<div class='span8'>Ваша корзина пуста</div>");
        }

    }
});
function removeFromBasket(id){
    tempItem = myBasket.get(id);
    tempItem.urlRoot = "/backbone/basket.php"
    myBasket.remove(tempItem);
    var basketView = new BasketView();
    basketView.el = $('#galleryContainer')[0];
    basketView.collection = myBasket;
    basketView.render();
    tempItem.destroy();

}

function buildBasket(){


    $('ul.nav li:not(.divider-vertical)').removeClass();
    $('ul.nav li:not(.divider-vertical):nth(3)').toggleClass("backgroundTexture upShadow");



    myBasket.url = "/backbone/basket.php";
    myBasket.fetch({
        error:function () {
        },
        success:function () {
            var basketView = new BasketView();
            basketView.el = $('#galleryContainer')[0];
            basketView.collection = myBasket;
            basketView.render();

        }
    });
}

function putToBasket(article, price1) {
    var tempBasket = new Basket({Article:article, price:price1});
    myBasket.add(tempBasket);
    tempBasket.url = "/backbone/basket.php"
    tempBasket.save();

    $('.top-left').notify({
        message: { text: 'Изделие успешно добавлено в корину' }
    }).show();
}